﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Cit.CDE.Net.Service.ModuleFactory;
using Cit.CDE.Net.Module;
using System.Collections.Generic;
using Cit.CDE.Net.Module.Commons;
using cde.SystemCommon;
using Cit.CDE.Net.Service;
using System.Text;

using Module;


/// <summary>
/// Copyright (C)   jinggubang
/// FileName:        LoreView.aspx.cs           
/// Author :         wangbc
/// CDT:             2012-02-01           
/// Version:         1.0.0.0                
/// Depiction:      资料库详细
/// </summary>
public partial class main_admin_LoreView : UserControl.CommonPage
{
    /// <summary>
    ///资料库EM
    /// </summary>
    private CDEEntityManager em;
    /// <summary>
    ///资料库Entity
    /// </summary>
    private CDEEntity m_entity;
    /// <summary>
    ///资料库Entity名字
    /// </summary>    
    private const string EM_FULL_NAME = WEBConstants.EM_LORE_MANAGE;

    //资料库ID
    public string chkID = "";

    /// <summary>
    /// 页面初始化
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {
        //资料库ID
        chkID = (string)Request.QueryString.Get("chkID");
        //当前登陆人信息
        LoginOprator op = (LoginOprator)Session[Cit.CDE.Net.Module.Commons.CDEConstants.SESSION_LOIN_OPRATOR];

        em = CDEEMFactory.getEMByFullName(EM_FULL_NAME);
        if (!Page.IsPostBack)
        {
            //ID不为空修改资料库，否则新增资料库
            if (chkID != "" && chkID != null)
            {
                //修改
                this.page_title.InnerText = "资料库详细";
                CDESearchCriteria sc = new CDESearchCriteria(em);
                sc.addCondition("ID", chkID);
                List<CDEEntity> listEntity = CDEService.LoadEntities(em, sc);
                //资料库ID是否存在
                if (listEntity.Count > 0)
                {
                    m_entity = listEntity[0];
                    m_entity.Operation = CDEDataOperation.UPDATE;

                    //类型名称设置
                    string code = Util.objectToString(m_entity.get("LORE_TYPE__TYPE_CODE"));
                    this.lbl_TYPE_NAME.Text = Util.getLoreTypeName(code);

                    //查看次数
                    //当前时间
                    string Now = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss").Replace("-", "/");
                    string downLoads = Util.objectToNumbString(int.Parse(Util.objectToNumbString(m_entity.get("VIEWS"))) + 1);
                    m_entity.set("VIEWS", downLoads);
                    m_entity.set("UPDATE_USER", op.SysID);
                    m_entity.set("UPDATE_DATE", Now);
                    m_entity.Operation = CDEDataOperation.UPDATE;
                    //listEntity.Add(m_entity);
                    CDEService.UpdateEntities(em, listEntity);
                }
                else
                {
                    m_entity = CDEEMServiceFactory.getEMService(em).CreateEntity();
                    m_entity.Operation = CDEDataOperation.INSERT;
                }
            }
            else
            {
                //新增
                this.page_title.InnerText = "资料库新增";
                m_entity = CDEEMServiceFactory.getEMService(em).CreateEntity();
                m_entity.Operation = CDEDataOperation.INSERT;
                
            }
            Session["MENTITY"] = m_entity;
            //资料库信息设值到页面
            this.setControls(m_entity, Resources.ResourceErrMsg.ResourceManager);
        }
        else
        {
            m_entity = (CDEEntity)Session["MENTITY"];
            //页面信息放入资料库Entity
            this.setEntityFromRequest(m_entity, Request.Params);
        }

        //返回显示
        string viewType = Util.objectToString((string)Request.QueryString.Get("viewType"));
        if (viewType != "")
        {
            this.Button3.Visible = false;
        }
        else
        {
            this.Button3.Visible = true;
        }
    }
    

    /// <summary>
    /// 返回按钮
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void cmdCancel_Click(object sender, EventArgs e)
    {
        if (Sel_MenuNode == null)
            return;
        StringBuilder url = new StringBuilder();
        url.Append("../admin/LoreListView.aspx?EM=");
        url.Append(EM_FULL_NAME);
        url.Append("&sel_menu_id=");
        url.Append(Sel_MenuNode.Attributes["id"].Value);
        url.Append("&IsBackSc=1");
        Response.Redirect(url.ToString());

    }
}
